LBITS := $(shell getconf LONG_BIT)
ifeq ($(LBITS),64)
   LD_PATH=externLibs/unix/64
else
   LD_PATH=externLibs/unix/32
endif

srcdir=src/*/
objdir=obj/
libdir=lib/
includeLibs=externLibs/
CXX=g++
CXXFLAGS=-fPIC -Wall -DGLEW_BUILD -s -O2
LDFLAGS=-L$(LD_PATH) -lglfw -lGL -lGLU -lSOIL #-lpthread
LIB=libEpix3D.so
SRC= $(wildcard $(srcdir)*.cpp)
OBJ= $(SRC:%.cpp=$(objdir)%.o)
INSTALL_PREFIX=/usr/
INSTALL_TARGET=Epix3D
INSTALL=cp

all: depend $(LIB)

$(LIB): $(OBJ)
	$(CXX) -o obj/externLibs/glew.o -c externLibs/GL/glew.c $(CXXFLAGS) -I$(includeLibs) -DGLEW_STATIC
	$(CXX) -shared -o $(libdir)$@ $^ obj/externLibs/glew.o $(LDFLAGS) $(CXXFLAGS)

depend: 
	$(CXX) -MM $(SRC) -I$(includeLibs) >depend

-include depend

$(objdir)%.o: %.cpp
	$(CXX) -o $@ -c $< $(CXXFLAGS) -I$(includeLibs)

.PHONY: install clean cleaninstall mrproper

install: uninstall
	mkdir -p $(INSTALL_PREFIX)include/$(INSTALL_TARGET)
	mkdir -p $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Config
	mkdir -p $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Core
	mkdir -p $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Debug
	mkdir -p $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Loaders
	mkdir -p $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Maths
	mkdir -p $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Utilities
	mkdir -p $(INSTALL_PREFIX)lib
	$(INSTALL) $(libdir)$(LIB) $(INSTALL_PREFIX)lib
	$(INSTALL) src/Epix3D.h $(INSTALL_PREFIX)include/$(INSTALL_TARGET)
	$(INSTALL) src/Config/*.h $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Config
	$(INSTALL) src/Core/*.h $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Core
	$(INSTALL) src/Debug/*.h $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Debug
	$(INSTALL) src/Loaders/*.h $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Loaders
	$(INSTALL) src/Maths/*.h $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Maths
	$(INSTALL) src/Utilities/*.h $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Utilities
	$(INSTALL) src/Core/*.inl $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Core
	$(INSTALL) src/Maths/*.inl $(INSTALL_PREFIX)include/$(INSTALL_TARGET)/Maths
	
uninstall:
	rm -rf $(INSTALL_PREFIX)include/$(INSTALL_TARGET)
	rm -f $(INSTALL_PREFIX)lib/$(LIB)
	
clean:
	rm -rf $(objdir)*/*.o
	rm -rf $(objdir)*/*/*.o

mrproper: clean
	rm -rf $(libdir)$(LIB)
	rm -rf $(includedir)*/*~
	rm -rf $(srcdir)*~
	rm -rf *~
